package xyz.msj.hrm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xyz.msj.hrm.constant.SMSConstant;
import xyz.msj.hrm.domain.Sso;
import xyz.msj.hrm.domain.VipBase;
import xyz.msj.hrm.dto.ResDto;
import xyz.msj.hrm.excptions.GlobalException;
import xyz.msj.hrm.feign.client.RedisFeignClient;
import xyz.msj.hrm.mapper.SsoMapper;
import xyz.msj.hrm.mapper.VipBaseMapper;
import xyz.msj.hrm.service.ISsoService;
import xyz.msj.hrm.util.AjaxResult;
import xyz.msj.hrm.util.StrUtils;
import xyz.msj.hrm.util.encrypt.MD5;
import xyz.msj.hrm.vo.SMSCodeSendVo;

import java.util.Date;

/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author mushengjie
 * @since 2020-09-17
 */
@Service
@Transactional
public class SsoServiceImpl extends ServiceImpl<SsoMapper, Sso> implements ISsoService {

    @Autowired
    private RedisFeignClient redisFeignClient;

    @Autowired
    private VipBaseMapper vipBaseMapper;

    /**
     * 注册
     *
     * @param resDto
     */
    @Override
    public void register(ResDto resDto) {
        //获取手机号参数
        String mobile = resDto.getMobile();
        //获取图片code
        String imageCode = resDto.getImageCode();
        //获取密码
        String password = resDto.getPassword();
        //获取短信验证码
        String smsCode = resDto.getSmsCode();
        //检查参数和判断
        checkparam(mobile, imageCode, password, smsCode);
        //给sso对象赋值
        Sso sso = setSsoparam(mobile, password);
        //首先保存sso用户表
        baseMapper.insert(sso);
        //给vipbase进行初始化
        VipBase vipBase = getVipBase(sso);
        //保存
        vipBaseMapper.insert(vipBase);
    }

    private VipBase getVipBase(Sso sso) {
        //4.保存base基本信息表
        VipBase vipBase = new VipBase();
        vipBase.setSsoId(sso.getId());
        vipBase.setCreateTime(new Date());
        vipBase.setRegChannel(1);   //1.pc断
        vipBase.setRegTime(new Date());
        return vipBase;
    }

    private Sso setSsoparam(String mobile, String password) {
        //创建用户对象
        Sso sso = new Sso();
        //创建时间
        sso.setCreateTime(new Date());
        //更新时间默认null
        sso.setUpdateTime(null);
        /**
         * 三方登录名
         */
        sso.setThirdUid(null);
        /**
         * 手机号
         */
        sso.setPhone(mobile);
        /**
         * 邮箱
         */
        sso.setEmail(null);
        /**
         * 盐值
         */
        String salt = StrUtils.getComplexRandomString(10);
        sso.setSalt(salt);
        /**
         * 密码 加盐
         */
        sso.setPassword(MD5.getMD5(password+salt));
        //设置参数绑定等级
        sso.addBitState(Sso.BIT_STATE_PHONE);

        return sso;
    }

    private void checkparam(String mobile, String imageCode, String password, String smsCode) {
        //判断手机号是否为null
        if (StringUtils.isEmpty(mobile)) {
            throw new GlobalException("手机号为空！");
        }
        //判断图片验证码
        if (StringUtils.isEmpty(imageCode)) {
            throw new GlobalException("图片验证码为空！");
        }
        //判断密码是否为null
        if (StringUtils.isEmpty(password)) {
            throw new GlobalException("密码为空！");
        }
        //判断短信验证码是否为null
        if (StringUtils.isEmpty(smsCode)) {
            throw new GlobalException("短信验证码为空！");
        }
        //判断手机号是否被注册
        Sso dbSso = baseMapper.selectByPhone(mobile);
        //判读通过手机查询到的对象不等于null
        if (dbSso != null) {
            throw new GlobalException("手机号已经被注册，请登录！");
        }
        //判断短信验证码是否过期或者无效
        AjaxResult smsCodeJson = redisFeignClient.get(SMSConstant.SMS_PHONE_REG + mobile);
        if (!smsCodeJson.isSuccess() || smsCodeJson.getResultObj() == null) {
            throw new GlobalException("验证码过期或者无效，请重新输入");
        }
        //将json字符串转换为字符串
        String smsCodeString = smsCodeJson.getResultObj().toString();
        //获取code
        String code = JSON.parseObject(smsCodeString, SMSCodeSendVo.class).getCode();
        //判断输入的值和redis中的值是否相等
        if (!code.toUpperCase().equals(smsCode.toUpperCase().trim())) {
            throw new GlobalException("手机验证码输入错误请重新输入");
        }
    }
}
